//
//  messagesViewController.m
//  ZhenPin
//
//  Created by tmosport on 14-7-8.
//  Copyright (c) 2014年 zhenpin. All rights reserved.
//

#import "messagesViewController.h"
#import "MBProgressHUD.h"

@interface messagesViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UIView *emptyFooterView;
}
@property (strong, nonatomic) IBOutlet UITableView *messagesTV;
@property (strong, nonatomic) NSArray *logResult;


@end

@implementation messagesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"消息中心";
    [self dataInit];
    
    
    [self.navigationController.navigationBar setBackIndicatorImage:
     [UIImage imageNamed:@"return"]];
    [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:
     [UIImage imageNamed:@"return"]];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
}

-(void)leftBarbuttonItemClicked:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dataInit{
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.dimBackground = NO;
    NSDictionary *params = @{@"memberid": _memberId,
                             @"access_token": _token,
                             @"status": @"0",
                             @"pagenumber": @"1"
                             };
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:APIURL@"myzhenpin/notice/getMyNoticeList.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        _logResult = (NSArray *)responseObject;
        if ([_logResult count]==0) {
            [self showNoDataViewWithKeyWord:@"暂无消息"];
        } else {
            [self hideNoDataView];
        }

        [self.messagesTV reloadData];
        [HUD hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [HUD hide:YES];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_logResult count];
}

-(void)showNoDataViewWithKeyWord:(NSString *)key {
    NSString *str = [NSString stringWithFormat:@"%@",key];
    NSMutableAttributedString *textStr = [[NSMutableAttributedString alloc] initWithString:str];
    //    [textStr addAttribute:NSForegroundColorAttributeName value:ColorFromRGB(0xff4400) range:NSMakeRange(6, key.length)];
    
    if (emptyFooterView) {
        UILabel *label = (UILabel*)[emptyFooterView viewWithTag:800];
        label.attributedText = textStr;
        emptyFooterView.hidden = NO;
        [self.view bringSubviewToFront:emptyFooterView];
    } else {
        int height;
        height = 528;
        emptyFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, height)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
        imageView.center = CGPointMake(emptyFooterView.frame.size.width/2, (emptyFooterView.frame.size.height-220)/2);
        imageView.image = [UIImage imageNamed:@"no02"];
        [emptyFooterView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, (emptyFooterView.frame.size.height-240)/2+imageView.frame.size.height, 320, 30)];
        label.textColor = RGB(51, 51, 51);
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = 800;
        label.attributedText = textStr;
        [emptyFooterView addSubview:label];
        emptyFooterView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:emptyFooterView];
    }
}
-(void)hideNoDataView {
    if (emptyFooterView) {
        emptyFooterView.hidden = YES;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *sectionDic = [_logResult objectAtIndex:indexPath.section];
    UITableViewCell *cell;
    BOOL success = [sectionDic[@"isRead"] boolValue];
    if (success){
        cell = nil;
        cell = [tableView dequeueReusableCellWithIdentifier:@"readCell"];
    }else{
        cell = nil;
        cell = [tableView dequeueReusableCellWithIdentifier:@"reuseCell"];
    }
    UILabel *typeL = (UILabel *)[cell viewWithTag:1001];
    UILabel *timeL = (UILabel *)[cell viewWithTag:1002];
    UILabel *contentL = (UILabel *)[cell viewWithTag:1003];
    typeL.text = sectionDic[@"title"];
    contentL.text = sectionDic[@"content"];
    
    NSTimeInterval eTime=[[NSString stringWithFormat:@"%@",sectionDic[@"createTime"]] doubleValue];//str是NSString类型
    NSDate *startTimeDate = [NSDate dateWithTimeIntervalSince1970:eTime];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy-MM-dd HH:mm";
    NSString *startDateString = [df stringFromDate:startTimeDate];
    timeL.text = startDateString;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *sectionDic = [_logResult objectAtIndex:indexPath.section];
    NSString *uid = [NSString stringWithFormat:@"%@",sectionDic[@"uid"]];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([uid length] == 0 ||[uid isEqual:[NSNull null]]) {
        return;
    }
    NSDictionary *params = @{@"memberid": _memberId,
                             @"access_token": _token,
                             @"uid": uid
                             };
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:APIURL@"myzhenpin/notice/readNotice.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self dataInit];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //DO:错误捕获
    }];

    
    

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
